<?php
require_once 'Sabai/Controller/ModelEntityList.php';

class Xigg_Admin_Plugin_List extends Sabai_Controller_ModelEntityList
{
    function Xigg_Admin_Plugin_List()
    {
        parent::Sabai_Controller_ModelEntityList('Plugin', array('properties' => array('name', 'version', 'active', 'priority')));
    }

    function &_onListEntities(&$entities, &$context)
    {
        $select = $context->request->getAsStr('select');
        $plugin_manager =& $this->_locator->getService('PluginManager');
        $installed = array();
        $local = $plugin_manager->getLocalPlugins(true);
        while ($plugin =& $entities->getNext()) {
            $plugin_name = $plugin->getLabel();
            if (isset($local[$plugin_name])) {
                $installed[$plugin_name] = $local[$plugin_name];
                unset($local[$plugin_name]);
            }
        }
        switch($select) {
            case 'local':
                $model =& $this->_locator->getService('Model');
                $entities =& $model->createCollection('Plugin');
                $installed = array();
                break;
            case 'installed':
                $local = array();
                break;
            default:
                $select = 'all';
                break;
        }
        $context->response->setVars(array(
                                      'local_plugins' => $local,
                                      'requested_select' => $select,
                                      'installed_plugins' => $installed
                                    ));
        $entities->rewind();
        return $entities;
    }
}